package com.tian.service.impl;

import com.alibaba.fastjson.JSON;
import com.tian.constant.RabbitMQConstant;
import com.tian.entity.ChargePayOrder;
import com.tian.enums.PayRetryMessageTypeEnum;
import com.tian.enums.PayStatusEnum;
import com.tian.mapper.ChargePayOrderMapper;
import com.tian.message.UserCouponMessage;
import com.tian.producer.UserUseCouponProducer;
import com.tian.service.QueryPayOrderService;
import com.wechat.pay.java.service.payments.h5.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * {@code @description:} 微信支付订单查询服务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-10-11 13:01
 * {@code @version:} 1.0
 */
@Service("queryWXPayOrderService")
public class QueryPayOrderServiceImpl implements QueryPayOrderService {
//    @Resource
//    private H5Service h5Service;
//    @Resource
//    private WXPayConfig wxPayConfig;
    @Resource
    private ChargePayOrderMapper chargePayOrderMapper;
    @Resource
    private UserUseCouponProducer userUseCouponProducer;

    @Override
    public void queryPayOrder() {
//        String merchantId = wxPayConfig.getMerchantId();
        String merchantId = "wx1234567890123456";
        List<ChargePayOrder> chargePayOrders = chargePayOrderMapper.selectAll();
        chargePayOrders.forEach(chargePayOrder -> {
            QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
            request.setMchid(merchantId);
            request.setOutTradeNo(chargePayOrder.getPayOrderNo());
//            Transaction transaction = h5Service.queryOrderByOutTradeNo(request);
            Transaction transaction = new Transaction();
            transaction.setTradeState(Transaction.TradeStateEnum.SUCCESS);
            Transaction.TradeStateEnum tradeState = transaction.getTradeState();
            if (Transaction.TradeStateEnum.SUCCESS.equals(tradeState)) {
                // 更新支付订单状态PAID
                chargePayOrder.setPayStatus(PayStatusEnum.PAID.getCode());

                UserCouponMessage userCouponMessage = new UserCouponMessage();
                userCouponMessage.setUserId(chargePayOrder.getUserId());
                String reqId = "USER_USED_COUPON_REQ_ID" + System.currentTimeMillis() + chargePayOrder.getUserId();
                userCouponMessage.setReqId(reqId);

                updateOrderStatus(chargePayOrder, userCouponMessage, RabbitMQConstant.USER_USE_COUPON_EXCHANGE, PayRetryMessageTypeEnum.USER_USED_COUPON.getType());
            } else if (Transaction.TradeStateEnum.REFUND.equals(tradeState)) {
                // 更新支付订单状态REFUND
                chargePayOrder.setPayStatus(PayStatusEnum.REFUND.getCode());

                UserCouponMessage userCouponMessage = new UserCouponMessage();
                userCouponMessage.setUserId(chargePayOrder.getUserId());
                String reqId = "FREE_USER_REFUND_COUPON_REQ_ID" + System.currentTimeMillis() + chargePayOrder.getUserId();
                userCouponMessage.setReqId(reqId);

                updateOrderStatus(chargePayOrder, userCouponMessage, RabbitMQConstant.FREE_USER_USE_COUPON_EXCHANGE, PayRetryMessageTypeEnum.FREE_USER_USED_COUPON.getType());
            } else {
                // 更新支付订单状态FAILED
                chargePayOrder.setPayStatus(PayStatusEnum.FAILED.getCode());

                UserCouponMessage userCouponMessage = new UserCouponMessage();
                userCouponMessage.setUserId(chargePayOrder.getUserId());
                String reqId = "FREE_USER_FAILED_COUPON_REQ_ID" + System.currentTimeMillis() + chargePayOrder.getUserId();
                userCouponMessage.setReqId(reqId);
                updateOrderStatus(chargePayOrder, userCouponMessage, RabbitMQConstant.FREE_USER_USE_COUPON_EXCHANGE, PayRetryMessageTypeEnum.FREE_USER_USED_COUPON.getType());
            }
        });
    }

    private void updateOrderStatus(ChargePayOrder chargePayOrder,
                                   UserCouponMessage userCouponMessage,
                                   String mqExchange, int retryMessageType) {
        chargePayOrderMapper.updateByPrimaryKey(chargePayOrder);
        userUseCouponProducer.sendMessage(JSON.toJSONString(userCouponMessage), mqExchange, retryMessageType);
    }
}
